const User = require('../models/User');
const { StatusCodes } = require('http-status-codes');

const getUsers = async (req, res) => {
   const users = await User.find();
   res.status(StatusCodes.OK).json({
      data: {
         list: users,
         total: users.length,
      },
   });
};

const getUser = async (req, res) => {
   const user = await User.findOne({
      _id: req.user.userId,
   });
   res.status(StatusCodes.OK).json({
      data: user,
   });
};

const updateUser = async (req, res) => {
   const user = await User.findOneAndUpdate(
      {
         _id: req.user.userId,
      },
      req.body,
      {
         new: true,
         runValidators: true,
      }
   ).select('-password');
   res.status(StatusCodes.OK).json({
      data: user,
      msg: '更新成功',
   });
};

module.exports = {
   getUsers,
   getUser,
   updateUser,
};
